﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.ComponentModel;

namespace DAO
{
    public class CustomerDAO
    {
        public CustomerDTO CreateCustomerDTOFromEntity(KhachHang khachHang)
        {
            CustomerDTO cusDto = new CustomerDTO
            {
                CustomerCode = khachHang.MaKhachHang,
                CustomerID = khachHang.KhachHangID,
                CustomerName = khachHang.HoTen,
                CustomerCMND = khachHang.CMND,
                CustomerPhone = khachHang.DienThoai ?? string.Empty,
                CustomerBirthday = (DateTime)khachHang.NgaySinh,
                CustomerAddress = khachHang.DiaChi ?? string.Empty,
                CustomerEmail = khachHang.Email ?? string.Empty,
                CustomerSex = (bool) khachHang.GioiTinh,
                CustomerBeginDay = (DateTime)khachHang.NgayThem,
                CustomerDebtMoney =(float) khachHang.TienNo,
                Note = khachHang.GhiChu ?? string.Empty,
                Status = bool.Parse(khachHang.SuDung.ToString())
            };
            return cusDto;
        }

        public int InsertCustomer(CustomerDTO cusDto)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                KhachHang cusInt = new KhachHang
                {
                    MaKhachHang = cusDto.CustomerCode,
                    HoTen = cusDto.CustomerName,
                    NgaySinh = cusDto.CustomerBirthday,
                    DiaChi = cusDto.CustomerAddress,
                    DienThoai = cusDto.CustomerPhone,
                    Email = cusDto.CustomerEmail,
                    GioiTinh = cusDto.CustomerSex,
                    CMND = cusDto.CustomerCMND,
                    NgayThem = cusDto.CustomerBeginDay,
                    TienNo = cusDto.CustomerDebtMoney,
                    GhiChu = cusDto.Note,
                    SuDung = true
                };

                if (ent.KhachHangs.Count(t => t.KhachHangID != null) > 0)
                {
                    cusInt.KhachHangID = ent.KhachHangs.Max(t => t.KhachHangID) + 1;
                }
                else
                {
                    cusInt.KhachHangID = 1;
                }

                ent.AddToKhachHangs(cusInt);

                DocSequenceDAO docSeqDAO = new DocSequenceDAO();
                docSeqDAO.UpdateNextDocSequence(DocSequence.Customer.ToString());

                ent.SaveChanges();

                return cusInt.KhachHangID;
            }
        }

        public List<CustomerDTO> GetCustomerList()
        {
            List<CustomerDTO> listCustomerDto = new List<CustomerDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<KhachHang> listKhachHang = ent.KhachHangs.Where(c => c.SuDung == true).ToList();
                foreach (KhachHang item in listKhachHang)
                {
                    CustomerDTO cusDto = CreateCustomerDTOFromEntity(item);
                    listCustomerDto.Add(cusDto);
                }
            }
            return listCustomerDto;
        }

        public BindingList<CustomerDTO> GetNewBindingListCustomer()
        {
            BindingList<CustomerDTO> listCustomerDto = new BindingList<CustomerDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<KhachHang> listKhachHang = ent.KhachHangs.Where(c => c.SuDung == true).ToList();
                foreach (KhachHang item in listKhachHang)
                {
                    CustomerDTO cusDto = CreateCustomerDTOFromEntity(item);
                    if (cusDto.CustomerSex)
                        cusDto.CustomerSexString = "Nam";
                    else
                        cusDto.CustomerSexString = "Nữ";
                    listCustomerDto.Add(cusDto);
                }
            }
            return listCustomerDto;
        }

        public bool UpdateCustomer(CustomerDTO cusDTO)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    KhachHang cusEnt = ent.KhachHangs.FirstOrDefault(c => c.KhachHangID == cusDTO.CustomerID);

                    cusEnt.HoTen = cusDTO.CustomerName;
                    cusEnt.NgaySinh = cusDTO.CustomerBirthday;
                    cusEnt.DiaChi = cusDTO.CustomerAddress;
                    cusEnt.DienThoai = cusDTO.CustomerPhone;
                    cusEnt.Email = cusDTO.CustomerEmail;
                    cusEnt.GioiTinh = cusDTO.CustomerSex;
                    cusEnt.CMND = cusDTO.CustomerCMND;
                    cusEnt.GhiChu = cusDTO.Note;
                    cusEnt.SuDung = cusDTO.Status;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateCustomerDebtMoney(int cusID, double debtMoney)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    KhachHang cusEnt = ent.KhachHangs.FirstOrDefault(c => c.KhachHangID == cusID);

                    cusEnt.TienNo = debtMoney;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateCustomerDebtFromInvoice(int ID, float total)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    KhachHang cusEnt = ent.KhachHangs.FirstOrDefault(c => c.KhachHangID == ID);

                    cusEnt.TienNo += total;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public CustomerDTO GetCustomerByID(int ID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    KhachHang customer = ent.KhachHangs.FirstOrDefault(c => c.KhachHangID == ID);

                    CustomerDTO cusDTO = CreateCustomerDTOFromEntity(customer);

                    if (cusDTO.CustomerSex)
                        cusDTO.CustomerSexString = "Nam";
                    else
                        cusDTO.CustomerSexString = "Nữ";

                    return cusDTO;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool DeleteCustomer(int ID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    KhachHang cusEnt = ent.KhachHangs.FirstOrDefault(c => c.KhachHangID == ID);
                    cusEnt.SuDung = false;
                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool CheckExistsCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int count = ent.KhachHangs.Where(c => c.MaKhachHang == code).Count();

                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return true;
            }
        }

        public CustomerDTO GetCustomerByCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    KhachHang kh = ent.KhachHangs.FirstOrDefault(k => k.MaKhachHang == code);

                    return CreateCustomerDTOFromEntity(kh);
                }
            }
            catch
            {
                return null;
            }
        }

        public List<CustomerReportDTO> GetListCustomerForReport(int month)
        {
            List<CustomerReportDTO> listReport = new List<CustomerReportDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<KhachHang> listCustomer = ent.KhachHangs.Where(kh => kh.SuDung == true).ToList();
                PayMoneyDAO payDAO = new PayMoneyDAO();
                InvoiceDAO invoiceDAO = new InvoiceDAO();

                foreach (KhachHang kh in listCustomer)
                {
                    CustomerReportDTO CusRpt = new CustomerReportDTO();
                    CusRpt.CustomerID = kh.KhachHangID;
                    CusRpt.CustomerCode = kh.MaKhachHang;
                    CusRpt.CustomerName = kh.HoTen;
                    CusRpt.CustomerAddress = kh.DiaChi;
                    CusRpt.CustomerCMND = kh.CMND;
                    CusRpt.CustomerPhone = kh.DienThoai;

                    DateTime now = DateTime.Now;
                    //neu la thang hien tai
                    if (month == now.Month)
                    {
                        CusRpt.EndDebt = (int)kh.TienNo;
                    }
                    else
                    {
                        int totalPay = 0;
                        int totalBuy = 0;
                        for (int m = month + 1; m <= month; m++)
                        {
                            totalPay += payDAO.GetTotalPay(kh.KhachHangID, m);
                            totalBuy += invoiceDAO.GetTotalBuy(kh.KhachHangID, m);
                        }
                        CusRpt.EndDebt = (int)kh.TienNo - (totalBuy - totalPay);
                    }

                    CusRpt.TotalPay = payDAO.GetTotalPay(kh.KhachHangID, month);
                    CusRpt.TotalBuy = invoiceDAO.GetTotalBuy(kh.KhachHangID, month);
                    CusRpt.FirstDebt = (CusRpt.TotalPay - CusRpt.TotalBuy) + CusRpt.EndDebt;

                    listReport.Add(CusRpt);
                }
            }

            return listReport;
        }

        public List<CustomerReportDTO> GetListCustomerForReport(int customerID, int month)
        {
            List<CustomerReportDTO> listReport = new List<CustomerReportDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<KhachHang> listCustomer = ent.KhachHangs.Where(kh => kh.SuDung == true && kh.KhachHangID == customerID).ToList();
                PayMoneyDAO payDAO = new PayMoneyDAO();
                InvoiceDAO invoiceDAO = new InvoiceDAO();

                foreach (KhachHang kh in listCustomer)
                {
                    CustomerReportDTO CusRpt = new CustomerReportDTO();
                    CusRpt.CustomerID = kh.KhachHangID;
                    CusRpt.CustomerCode = kh.MaKhachHang;
                    CusRpt.CustomerName = kh.HoTen;
                    CusRpt.CustomerAddress = kh.DiaChi;
                    CusRpt.CustomerCMND = kh.CMND;
                    CusRpt.CustomerPhone = kh.DienThoai;

                    DateTime now = DateTime.Now;
                    //neu la thang hien tai
                    if (month == now.Month)
                    {
                        CusRpt.EndDebt = (int)kh.TienNo;
                    }
                    else
                    {
                        int totalPay = 0;
                        int totalBuy = 0;
                        for (int m = month + 1; m <= month; m++)
                        {
                            totalPay += payDAO.GetTotalPay(kh.KhachHangID, m);
                            totalBuy += invoiceDAO.GetTotalBuy(kh.KhachHangID, m);
                        }
                        CusRpt.EndDebt = (int)kh.TienNo - (totalBuy - totalPay);
                    }

                    CusRpt.TotalPay = payDAO.GetTotalPay(kh.KhachHangID, month);
                    CusRpt.TotalBuy = invoiceDAO.GetTotalBuy(kh.KhachHangID, month);
                    CusRpt.FirstDebt = (CusRpt.TotalPay - CusRpt.TotalBuy) + CusRpt.EndDebt;

                    listReport.Add(CusRpt);
                }
            }

            return listReport;
        }
    }
}
